package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysWechatMapper;
import com.settlement.system.model.entity.SysWechat;
import com.settlement.system.model.form.SysWechatForm;
import com.settlement.system.model.query.SysWechatQuery;
import com.settlement.system.model.vo.SysWechatImportVo;
import com.settlement.system.service.SysWechatService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员微信表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-03-11
 */
@Service
public class SysWechatServiceImpl extends ServiceImpl<SysWechatMapper, SysWechat> implements SysWechatService {

    @Override
    public Page<SysWechat> getPage(SysWechatQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysWechat> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysWechat(SysWechatForm sysWechatForm) {
        SysWechat sysWechat = new SysWechat();
        BeanUtil.copyProperties(sysWechatForm, sysWechat);
        sysWechat.setCreateTime(null);
        return this.saveOrUpdate(sysWechat);
    }

    @Override
    public SysWechatForm getSysWechatFormData(Long id) {
        SysWechat sysWechat = this.getById(id);
        SysWechatForm sysWechatForm = new SysWechatForm();
        BeanUtil.copyProperties(sysWechat, sysWechatForm);
        return sysWechatForm;
    }

    @Override
    public boolean deleteSysWechat(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysWechatImportVo> voList) {
        List<SysWechat> sysWechatList = new ArrayList<>();
        for (SysWechatImportVo importVo : voList) {
            SysWechat sysWechat = new SysWechat();
            BeanUtil.copyProperties(importVo, sysWechat);
            sysWechatList.add(sysWechat);
        }
        this.saveOrUpdateBatch(sysWechatList);
    }

    @Override
    public List<SysWechatImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysWechat> sysWechatList = this.getBaseMapper().selectBatchIds(ids);
        List<SysWechatImportVo> voList = new ArrayList<>();
        for (SysWechat sysWechat : sysWechatList) {
            SysWechatImportVo vo = new SysWechatImportVo();
            BeanUtil.copyProperties(sysWechat, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public boolean enable(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "启用的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        List<SysWechat> sysWechats = this.baseMapper.selectBatchIds(idArray);
        for (SysWechat sysWechat : sysWechats) {
            sysWechat.setUseStatus(1);
        }
        return this.saveOrUpdateBatch(sysWechats);
    }

    @Override
    public boolean disable(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "禁用的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        List<SysWechat> sysWechats = this.baseMapper.selectBatchIds(idArray);
        for (SysWechat sysWechat : sysWechats) {
            sysWechat.setUseStatus(0);
        }
        return this.saveOrUpdateBatch(sysWechats);
    }
}
